<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/home/top_logo.png" alt="" />
        <span>京淘电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409bff"
          :unique-opened="true"
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标根据循环动态获取图标对象数组的id获取对应图标 -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容区 -->
      <el-main>
          <!-- 主页面路由占位符 -->
          <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      //左侧菜单数据
      menulist: [
        {
          authName: '商品管理',
          children: [
            {
              authName: '新增商品',
              children: '',
              id: 11,
              order: null,
              path: 'item-add'
            },
            {
              authName: '查询商品',
              children: '',
              id: 12,
              order: null,
              path: 'item-list'
            },
            {
              authName: '规格参数',
              children: '',
              id: 13,
              order: null,
              path: 'item-param-list'
            }
          ],
          id: 1,
          order: 1,
          path: 'users'
        },
        {
          authName: '网站内容管理',
          children: [
            {
              authName: '内容分类管理',
              children: '',
              id: 21,
              order: null,
              path: ''
            },
            {
              authName: '查询商品',
              children: '',
              id: 22,
              order: null,
              path: ''
            }
          ],
          id: 2,
          order: 2,
          path: 'users'
        }
      ],
      iconsObj: {
        '1': 'el-icon-shopping-cart-1',
        '2': 'el-icon-mobile'
      },
      isCollapse: false
    }
  },
  //生命周期函数
  created() {
    //获取菜单栏列表
    // this.getMenuList()
  },
  methods: {
    //退出登录
    logout() {
      //清空头ken
      window.sessionStorage.clear()
      this.$router.push((name = 'login'))
    },
    //获取所有的菜单
    // async getMenuList() {
    //   const { data: res } = await this.$axios.get('menus')
    //   if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
    //   this.menulist = res.data
    //   console.log(res)
    // }
    //点击展开菜单框
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    img {
      width: 10%;
      height: 60px;
    }
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  -webkit-transition-duration: 300ms;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}
.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
